package com.zygh.policeasset.utils;

import com.github.junrar.Archive;
import com.github.junrar.rarfile.FileHeader;
import com.zygh.policeasset.pojo.ZipOrRar;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Enumeration;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;

import static java.awt.image.ImageObserver.ERROR;

import static jdk.nashorn.internal.runtime.regexp.joni.encoding.CharacterType.BLANK;
import static org.apache.poi.ss.usermodel.DataValidationConstraint.ValidationType.FORMULA;
import static org.aspectj.apache.bcel.classfile.annotation.ElementValue.STRING;

/* loaded from: police-asset-2.0-0.0.1-SNAPSHOT.jar:BOOT-INF/classes/com/zygh/policeasset/utils/UnZipAndRarUtil.class */
public class UnZipAndRarUtil {
    public static ZipOrRar unZip(String zipPath, String outDir) throws Exception {
        OutputStream out = null;
        InputStream in = null;
        ZipFile zip = null;
        File zipFile = new File(zipPath);
        File outFile = new File(outDir);
        if (!outFile.exists()) {
            outFile.mkdirs();
        }
        try {
            int num = 0;
            String zipOutPath = "";
            String relativePaths = "";
            ZipOrRar zipOrRar = new ZipOrRar();
            try {
                zip = new ZipFile(zipFile, String.valueOf(Charset.forName("gbk")));
                Enumeration<? extends ZipEntry> entries = zip.getEntries();
                while (entries.hasMoreElements()) {
                    ZipEntry entry = entries.nextElement();
                    String zipEntryName = entry.getName();
                    in = zip.getInputStream(entry);
                    String outPath = outDir + zipEntryName;
                    File file = new File(outPath.substring(0, outPath.lastIndexOf("/")));
                    if (!file.exists()) {
                        file.mkdirs();
                    }
                    if (!new File(outPath).isDirectory()) {
                        out = new FileOutputStream(outPath);
                        byte[] buff = new byte[4096];
                        while (true) {
                            int len = in.read(buff);
                            if (len <= 0) {
                                break;
                            }
                            out.write(buff, 0, len);
                        }
                        in.close();
                        out.close();
                        String[] zipArr = zipEntryName.split("/");
                        String zipName = zipArr[zipArr.length - 1];
                        if (zipName.contains(".xlsx") || zipName.contains(".xls")) {
                            zipOrRar.setExcelName(zipName);
                        }
                        if (num == 0) {
                            String[] outArr = outPath.split("/");
                            for (int i = 0; i < outArr.length - 1; i++) {
                                zipOutPath = zipOutPath + outArr[i] + "/";
                            }
                            for (int i2 = 1; i2 < outArr.length - 1; i2++) {
                                relativePaths = relativePaths + outArr[i2] + "/";
                            }
                            zipOrRar.setFilePath(zipOutPath);
                            zipOrRar.setRelativePaths(relativePaths);
                            num++;
                        }
                    }
                }
                if (zip != null) {
                    zip.close();
                }
                if (in != null) {
                    in.close();
                }
                if (out != null) {
                    out.close();
                }
                return zipOrRar;
            } catch (Exception e) {
                e.printStackTrace();
                if (zip != null) {
                    zip.close();
                }
                if (in != null) {
                    in.close();
                }
                if (out != null) {
                    out.close();
                }
                return null;
            }
        } catch (Throwable th) {
            if (zip != null) {
                zip.close();
            }
            if (in != null) {
                in.close();
            }
            if (out != null) {
                out.close();
            }
            throw th;
        }
    }

    public static ZipOrRar unRar(String rarPath, String outDir) throws Exception {
        String outName;
        File rarFile = new File(rarPath);
        File outFileDir = new File(outDir);
        if (!outFileDir.exists() && outFileDir.mkdirs()) {
            System.out.println("创建压缩目录成功");
        }
        Archive archive = new Archive(new FileInputStream(rarFile));
        FileHeader fileHeader = archive.nextFileHeader();
        ZipOrRar zipOrRar = new ZipOrRar();
        int num = 0;
        String rarOutPath = "";
        String relativePaths = "";
        while (fileHeader != null) {
            try {
                try {
                    if (fileHeader.isDirectory()) {
                        fileHeader = archive.nextFileHeader();
                    } else {
                        if (fileHeader.isUnicode()) {
                            outName = outDir + fileHeader.getFileNameW().trim().replaceAll("\\\\", "/");
                        } else {
                            outName = outDir + fileHeader.getFileNameString().trim().replaceAll("\\\\", "/");
                        }
                        File out = new File(outName);
                        if (!out.exists()) {
                            if (!out.getParentFile().exists()) {
                                out.getParentFile().mkdirs();
                            }
                            out.createNewFile();
                        }
                        FileOutputStream os = new FileOutputStream(out);
                        archive.extractFile(fileHeader, os);
                        os.close();
                        fileHeader = archive.nextFileHeader();
                        String[] araArr = outName.split("/");
                        String rarName = araArr[araArr.length - 1];
                        if (rarName.contains(".xlsx") || rarName.contains(".xls")) {
                            zipOrRar.setExcelName(rarName);
                        }
                        if (num == 0) {
                            for (int i = 0; i < araArr.length - 1; i++) {
                                rarOutPath = rarOutPath + araArr[i] + "/";
                            }
                            for (int i2 = 1; i2 < araArr.length - 1; i2++) {
                                relativePaths = relativePaths + araArr[i2] + "/";
                            }
                            zipOrRar.setFilePath(rarOutPath);
                            zipOrRar.setRelativePaths(relativePaths);
                            num++;
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    if (archive != null) {
                        archive.close();
                    }
                    return null;
                }
            } catch (Throwable th) {
                if (archive != null) {
                    archive.close();
                }
                throw th;
            }
        }
        archive.close();
        if (archive != null) {
            archive.close();
        }
        return zipOrRar;
    }

    public static Sheet readExcel(String ExcelPath) throws Exception {
        Workbook workbook = null;
        File excelFile = new File(ExcelPath.trim());
        InputStream is = new FileInputStream(excelFile);
        if (excelFile.getName().endsWith("xlsx")) {
            workbook = new XSSFWorkbook(is);
        } else if (excelFile.getName().endsWith("xls")) {
            workbook = new HSSFWorkbook(is);
        }
        if (workbook == null) {
            System.err.println("Excel文件有问题,请检查！");
        }
        Sheet sheet = workbook.getSheetAt(0);
        is.close();
        return sheet;
    }

    public static Sheet[] readZnExcel(String ExcelPath) throws Exception {
        Workbook workbook = null;
        File excelFile = new File(ExcelPath.trim());
        InputStream is = new FileInputStream(excelFile);
        if (excelFile.getName().endsWith("xlsx")) {
            workbook = new XSSFWorkbook(is);
        } else if (excelFile.getName().endsWith("xls")) {
            workbook = new HSSFWorkbook(is);
        }
        if (workbook == null) {
            System.err.println("Excel文件有问题,请检查！");
        }
        Sheet[] sheets = {workbook.getSheetAt(0), workbook.getSheetAt(1)};
        is.close();
        return sheets;
    }

    public static String getStringValue(Cell cell) {
        SimpleDateFormat sdf;
        if (cell == null) {
            return null;
        }
        switch (cell.getCellType()) {
            case STRING:
                return cell.getStringCellValue();
           /* case NUMERIC:
                if (HSSFDateUtil.isCellDateFormatted(cell)) {
                    if (cell.getCellStyle().getDataFormat() == HSSFDataFormat.getBuiltinFormat("h:mm")) {
                        sdf = new SimpleDateFormat("HH:mm");
                    } else if (cell.getCellStyle().getDataFormat() == 178 || cell.getCellStyle().getDataFormat() == 177) {
                        sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    } else {
                        sdf = new SimpleDateFormat("yyyy-MM-dd");
                    }
                    return String.valueOf(sdf.format(cell.getDateCellValue()));
                } else if (cell.getCellStyle().getDataFormat() == 58) {
                    return String.valueOf(new SimpleDateFormat("yyyy-MM-dd").format(DateUtil.getJavaDate(cell.getNumericCellValue())));
                } else {
                    double value = cell.getNumericCellValue();
                    CellStyle style = cell.getCellStyle();
                    DecimalFormat format = new DecimalFormat();
                    if (style.getDataFormatString().equals("General")) {
                        format.applyPattern("#");
                    }
                    return String.valueOf(Math.round(value));
                }
            case BOOLEAN:
                return String.valueOf(cell.getBooleanCellValue());*/
            case FORMULA:
                return String.valueOf(Double.valueOf(cell.getNumericCellValue()).intValue());
            case BLANK:
                return "";
            case ERROR:
                return "非法字符";
            default:
                return "未知类型";
        }
    }

    public static void delete(File path) {
        if (null != path && path.exists()) {
            if (path.isFile()) {
                Boolean result = Boolean.valueOf(path.delete());
                int tryCount = 0;
                while (!result.booleanValue()) {
                    tryCount++;
                    if (tryCount >= 10) {
                        break;
                    }
                    System.gc();
                    result = Boolean.valueOf(path.delete());
                }
            }
            File[] files = path.listFiles();
            if (null != files) {
                for (File file : files) {
                    delete(file);
                }
            }
            path.delete();
        }
    }
}